home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STDLIB.PAK / PNT2FNCT.CPP < prev    next >
Text File  |  1997-05-06  |  1KB  |  47 lines

  1.  #include<functional>
  2.  #include<deque>
  3.  #include<vector>
  4.  #include<algorithm>
  5.  
  6.  //
  7.  // Create a function.
  8.  //
  9.  int factorial (int x)
  10.  {
  11.    int result = 1;
  12.    for(int i = 2; i <= x; i++)
  13.      result *= i;
  14.    return result;
  15.  }
  16.  
  17.  using namespace std;
  18.  
  19.  int main ()
  20.  {
  21.    //
  22.    // Initialize a deque with an array of integers.
  23.    //
  24.    int init[7] = {1,2,3,4,5,6,7};
  25.    deque<int> d(init+0, init+7);
  26.    //
  27.    // Create an empty vector to store the factorials.
  28.    //
  29.    vector<int> v((size_t)7);
  30.    //
  31.    // Transform the numbers in the deque to their factorials and store
  32.    // in the vector.
  33.    //
  34.    transform(d.begin(), d.end(), v.begin(), ptr_fun(factorial));
  35.    //
  36.    // Print the results.
  37.    //
  38.    cout << "The following numbers: " << endl << "     ";
  39.    copy(d.begin(), d.end(), ostream_iterator<int>(cout," "));
  40.  
  41.    cout << endl << endl;
  42.    cout << "Have the factorials: " << endl << "     ";
  43.    copy(v.begin(), v.end(), ostream_iterator<int>(cout," "));
  44.  
  45.    return 0;
  46.  }
  47.